//
//  CatalogViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/5.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CatalogViewController.h"
#import "CatalogCell.h"
#import "OpusListModel.h"
#define defaultTag 1990
@interface CatalogViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong) CABasicAnimation * anim;
@property (nonatomic,strong) CAReplicatorLayer *musicLayer;

@property(nonatomic,assign)BOOL isShow;

@property (nonatomic, assign) NSInteger btnTag;//默认选中的Tag
@end

@implementation CatalogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
   
    if ( [[NSUserDefaults standardUserDefaults] objectForKey:@"saveposition"]) {
        self.btnTag = [[[NSUserDefaults standardUserDefaults] objectForKey:@"saveposition"] integerValue];
    }else{
        self.btnTag = defaultTag + 0;
    }
    
     [self createTableviewUI];
}
-(void)createTableviewUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - (64 + 40 + HEIGHT/3)) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CatalogCell class] forCellReuseIdentifier:@"CatalogCell"];
    _isShow = YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CatalogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CatalogCell"];
    OpusListModel *model= self.dataArray[indexPath.row];
    cell.titleLabel.text = model.name;
    cell.selectBtn.tag = defaultTag+indexPath.row;
    if (cell.selectBtn.tag == self.btnTag) {
        cell.isSelect = YES;
        cell.musicLayer.hidden = NO;
    }else{
        cell.isSelect = NO;
        cell.musicLayer.hidden = YES;
    }
    __weak CatalogCell *weakCell = cell;
    [weakCell setQhxSelectBlock:^(BOOL choice,NSInteger btnTag){
        if (choice) {
            self.btnTag = btnTag;
            [self clickDetailAndReloadData:indexPath.row];
            NSLog(@"$$$$$$%ld",(long)btnTag);
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)btnTag] forKey:@"saveposition"];
            [self.tableView reloadData];
        }
        else{
            //选中一个之后，再次点击，是未选中状态，图片仍然设置为选中的图片，记录下tag，刷新tableView，这个else 也可以注释不用，tag只记录选中的就可以
            self.btnTag = btnTag;
//            [self clickDetailAndReloadData:indexPath.row];
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)btnTag] forKey:@"saveposition"];
            [self.tableView reloadData];
            NSLog(@"#####%ld",(long)btnTag);
        }
        }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return scaleTopAndBottom(60);
}


-(void)clickDetailAndReloadData:(NSInteger)tagg{
    NSLog(@"你点击了第%ld个视频",(long)tagg);
    OpusListModel *model = self.dataArray[tagg];
    if (self.delegate && [self.delegate respondsToSelector:@selector(GetNewDataAndReload:)]) {
        [self.delegate GetNewDataAndReload:model.filepath];
        //        [self.tableView reloadData];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"acceptInforIdAndRefresh" object:nil userInfo:@{@"opusID":model.opusId}];

}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"点击了这个cell");
//    OpusListModel *model = self.dataArray[indexPath.row];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(GetNewDataAndReload:)]) {
//        [self.delegate GetNewDataAndReload:model.opusId];
//    }
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"acceptInforIdAndRefresh" object:nil userInfo:@{@"opusID":model.opusId}];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
